package com.ruiysoft.controller.aftersaleconf;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruiysoft.controller.aftersaleconf.vo.AftersaleConfVo;
import com.ruiysoft.controller.mdse.vo.SkuListVo;
import com.ruiysoft.entity.aftersaleconf.AftersaleConf;
import com.ruiysoft.entity.aftersaleconf.AftersaleConfSpuRela;
import com.ruiysoft.entity.mdse.MerchandiseSku;
import com.ruiysoft.entity.mdse.MerchandiseSpu;
import com.ruiysoft.pub.core.Result;
import com.ruiysoft.pub.core.ResultGenerator;
import com.ruiysoft.service.aftersaleconf.IAftersaleConfService;
import com.ruiysoft.service.aftersaleconf.IAftersaleConfSpuRelaService;
import com.ruiysoft.service.mdse.IMerchandiseSkuService;
import com.ruiysoft.service.mdse.IMerchandiseSpuService;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2018-11-24T21:35:29.769+08:00")

@RestController
public class InnrAftersaleConfApiController implements InnrAftersaleConfApi {

    private static final Logger log = LoggerFactory.getLogger(InnrAftersaleConfApiController.class);

    private final IAftersaleConfSpuRelaService spuRelaService;
    private final IAftersaleConfService confService;
    private final IMerchandiseSkuService skuService;
    private final IMerchandiseSpuService spuService;

    @org.springframework.beans.factory.annotation.Autowired
    public InnrAftersaleConfApiController(IAftersaleConfSpuRelaService spuRelaService, IAftersaleConfService confService, IMerchandiseSkuService skuService, IMerchandiseSpuService spuService) {
        this.spuRelaService = spuRelaService;
        this.confService = confService;
        this.skuService = skuService;
        this.spuService = spuService;
    }

    @Override
    public Result<Map<Long,AftersaleConfVo>> innrAftersaleConfGetConfBySpuIdPost(@ApiParam(value = ""  )  @Valid @RequestBody List<Long> spuIds) {
        try {
            List<MerchandiseSpu> spuList = (List<MerchandiseSpu>) spuService.listByIds(spuIds);
            Map<Long,MerchandiseSpu> spuMap = spuList.stream().collect(Collectors.toMap(spu->spu.getSpuId(), spu->spu));
//            Set<Long> spuIds = skuList.stream().map(sku->sku.getSpuId()).collect(Collectors.toSet());
            List<AftersaleConfSpuRela> relaList = (List<AftersaleConfSpuRela>) spuRelaService.listByIds(spuIds);
            Map<Long,AftersaleConf> spuConfMap = null;
            if(!relaList.isEmpty()){
                //查询有关联的设置
                Set<Long> confIds = relaList.stream().map(rela->rela.getAftersaleConfId()).collect(Collectors.toSet());
                List<AftersaleConf> confList = (List<AftersaleConf>) confService.listByIds(confIds);
                Map<Long,AftersaleConf> idMap = confList.stream().collect(Collectors.toMap(conf->conf.getId(),conf->conf));
//                Map<Long,AftersaleConfSpuRela> relaMap = relaList.stream().collect(Collectors.toMap(rela->rela.getSpuId(),rela->rela));
                spuConfMap = relaList.stream().collect(Collectors.toMap(rela->rela.getSpuId(),rela->idMap.get(rela.getAftersaleConfId())));
            }
            Map<Long,AftersaleConf> shopConfMap = null;
            if(relaList.isEmpty()||relaList.size()<spuIds.size()){
                //需要查询店铺默认设置
                Set<Long> shopIds = relaList.stream().map(rela->rela.getShopId()).collect(Collectors.toSet());
                List<AftersaleConf> confList = confService.list(new QueryWrapper<AftersaleConf>().in("shop_id",shopIds).eq("is_default",true));
                shopConfMap = confList.stream().collect(Collectors.toMap(conf->conf.getShopId(),conf->conf));
            }

            List<SkuListVo> list = new ArrayList<>();
            Map<Long, AftersaleConf> finalSpuConfMap = spuConfMap;
            Map<Long, AftersaleConf> finalShopConfMap = shopConfMap;
            Map<Long,AftersaleConfVo> map = spuIds.stream()
                    .collect(Collectors.toMap(spuId->spuId, spuId->getAftersaleConfVoBySpuId(spuId, spuMap,finalSpuConfMap, finalShopConfMap)));
            return ResultGenerator.genSuccessResult(map);
        } catch (Exception e) {
            log.error("订单spu查询退换货模板--{}",JSON.toJSONString(spuIds), e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }



    @Override
    public Result<Map<Long,AftersaleConfVo>> innrAftersaleConfGetConfPost(@ApiParam(value = ""  )  @Valid @RequestBody List<Long> skuIds) {
        try {
            List<MerchandiseSku> skuList = (List<MerchandiseSku>) skuService.listByIds(skuIds);
            Map<Long,MerchandiseSku> skuMap = skuList.stream().collect(Collectors.toMap(sku->sku.getSkuId(),sku->sku));
            Set<Long> spuIds = skuList.stream().map(sku->sku.getSpuId()).collect(Collectors.toSet());
            List<AftersaleConfSpuRela> relaList = (List<AftersaleConfSpuRela>) spuRelaService.listByIds(spuIds);
            Map<Long,AftersaleConf> spuConfMap = null;
            if(!relaList.isEmpty()){
                //查询有关联的设置
                Set<Long> confIds = relaList.stream().map(rela->rela.getAftersaleConfId()).collect(Collectors.toSet());
                List<AftersaleConf> confList = (List<AftersaleConf>) confService.listByIds(confIds);
                Map<Long,AftersaleConf> idMap = confList.stream().collect(Collectors.toMap(conf->conf.getId(),conf->conf));
//                Map<Long,AftersaleConfSpuRela> relaMap = relaList.stream().collect(Collectors.toMap(rela->rela.getSpuId(),rela->rela));
                spuConfMap = relaList.stream().collect(Collectors.toMap(rela->rela.getSpuId(),rela->idMap.get(rela.getAftersaleConfId())));
            }
            Map<Long,AftersaleConf> shopConfMap = null;
            if(relaList.isEmpty()||relaList.size()<spuIds.size()){
                //需要查询店铺默认设置
                Set<Long> shopIds = skuList.stream().map(sku->sku.getShopId()).collect(Collectors.toSet());
                List<AftersaleConf> confList = confService.list(new QueryWrapper<AftersaleConf>().in("shop_id",shopIds).eq("is_default",true));
                shopConfMap = confList.stream().collect(Collectors.toMap(conf->conf.getShopId(),conf->conf));
            }

            List<SkuListVo> list = new ArrayList<>();
            Map<Long, AftersaleConf> finalSpuConfMap = spuConfMap;
            Map<Long, AftersaleConf> finalShopConfMap = shopConfMap;
            Map<Long,AftersaleConfVo> map = skuIds.stream()
                    .collect(Collectors.toMap(skuId->skuId, skuId->getAftersaleConfVo(skuId,skuMap, finalSpuConfMap, finalShopConfMap)));
            return ResultGenerator.genSuccessResult(map);
        } catch (Exception e) {
            log.error("订单sku查询退换货模板--{}",JSON.toJSONString(skuIds), e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }

    private AftersaleConfVo getAftersaleConfVo(Long skuId, Map<Long,MerchandiseSku> skuMap, Map<Long,AftersaleConf> finalSpuConfMap, Map<Long,AftersaleConf> finalShopConfMap) {
        AftersaleConf conf = null;
        MerchandiseSku sku = skuMap.get(skuId);
        if(sku!=null){
            conf = finalSpuConfMap!=null?finalSpuConfMap.get(sku.getSpuId()):null;
            conf = conf!=null?conf:(finalShopConfMap!=null?finalShopConfMap.get(sku.getShopId()):null);
        }
        if(conf==null){
            //默认设置
            conf = new AftersaleConf().setReturnedDay(7).setExchangeDay(15);
        }
        return aftersaleConfToVo(conf);
    }
    private AftersaleConfVo getAftersaleConfVoBySpuId(Long spuId, Map<Long, MerchandiseSpu> spuMap, Map<Long, AftersaleConf> finalSpuConfMap, Map<Long, AftersaleConf> finalShopConfMap) {
        AftersaleConf conf = null;
        MerchandiseSpu spu = spuMap.get(spuId);
        if(spu!=null){
            conf = finalSpuConfMap!=null?finalSpuConfMap.get(spu.getSpuId()):null;
            conf = conf!=null?conf:(finalShopConfMap!=null?finalShopConfMap.get(spu.getShopId()):null);
        }
        if(conf==null){
            //默认设置
            conf = new AftersaleConf().setReturnedDay(7).setExchangeDay(15);
        }
        return aftersaleConfToVo(conf);
    }
    AftersaleConfVo aftersaleConfToVo(AftersaleConf aftersaleConf){
        if(aftersaleConf==null){
            return new AftersaleConfVo();
        }

        return new AftersaleConfVo().name(aftersaleConf.getName()).id(aftersaleConf.getId()).exchangeDay(aftersaleConf.getExchangeDay()).isDefault(aftersaleConf.getDefault()).returnedDay(aftersaleConf.getReturnedDay());
    }
}
